package com.i.userservice.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.i.piccommon.config.MinioConfig;
import com.i.piccommon.constant.ApiResponse;
import com.i.piccommon.constant.RedisConstants;
import com.i.piccommon.domain.User;
import com.i.piccommon.exception.BusinessException;
import com.i.piccommon.util.JWTUtils;
import com.i.piccommon.util.MinioUtil;
import com.i.piccommon.util.UserContext;
import com.i.userservice.config.MQConfig;
import com.i.userservice.domain.vo.UserVO;
import com.i.userservice.service.IUserService;
import io.minio.MinioClient;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.Email;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;

/**
 * @author lewis
 * @type class
 * @description
 * @since 2024/10/24
 */
@RequiredArgsConstructor
@RestController
@Slf4j
public class UserController {
    private final RedisTemplate<String,String> redisTemplate;
    private final RabbitTemplate rabbitTemplate;
    private final MinioUtil minioUtil;
    private final IUserService userService;
    @GetMapping("/code/{email}")
    public ApiResponse<Boolean> getCode(@Email(regexp = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$") @PathVariable String email) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(MQConfig.EXCHANGE_NAME,MQConfig.ROUTING_KEY,email,correlationData);
        return ApiResponse.ok(true);
    }
    @PostMapping("/registry")
    public ApiResponse<Boolean> registry(@RequestBody UserVO userVO) {
        String s = redisTemplate.opsForValue().get(RedisConstants.REGISTER_CODE_KEY_PREFIX + userVO.getEmail());
        if(s != null && s.equals(userVO.getCode())) {
            Boolean registried = userService.registry(BeanUtil.copyProperties(userVO, User.class));
            if(registried) {
                return ApiResponse.ok(BooleanUtil.isTrue(registried));
            }
        }
        return ApiResponse.fail("注册失败");
    }
    @PostMapping("/login")
    public ApiResponse<User> login(@RequestBody User user) {
        User login = userService.login(user);
        if(login != null) {
            return ApiResponse.ok(login);
        }
        return ApiResponse.fail("邮箱地址或者密码错误");
    }
    @GetMapping("/info")
    public ApiResponse<User> info() throws ParseException {
        Long userId = UserContext.getUser();
        return ApiResponse.ok(userService.info(userId));
    }
    @PutMapping("/edit")
    public ApiResponse<User> edit(@RequestBody User user) throws ParseException {
        return ApiResponse.ok(userService.edit(user));
    }
    @DeleteMapping("/logoff")
    public ApiResponse<Boolean> logoff() throws ParseException {
        Long userId = UserContext.getUser();
        boolean b = userService.logoff(userId);
        if(b){
            return ApiResponse.ok(true);
        }else {
            return ApiResponse.fail("注销失败");
        }
    }
    @PutMapping("/avatar")
    public ApiResponse<User> updateAvatar(HttpServletRequest request,@RequestPart MultipartFile file) {
        Long userId = UserContext.getUser();
        String avatarName = UUID.randomUUID().toString(true);
        minioUtil.upload(file, avatarName);
        User user = userService.info(userId);
        user.setAvatar(minioUtil.getFileUrl(avatarName));
        User userEdit = userService.edit(user);
        return ApiResponse.ok(userEdit);
    }
}
